package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result selectList(UserDto userDto){
        log.info("查询用户{}",userDto);
        PageResult<User> list =userService.selectList(userDto);
        return Result.success(list);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户{}",user);
        userService.add(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户{}",ids);
        userService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询用户{}",id);
        User user = userService.selectById(id);
        return Result.success(user);
    }

    @PutMapping
    public Result update(@RequestBody User user){
        log.info("修改用户{}",user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/role/{roleLabel}")
    public Result selectByRole(@PathVariable String roleLabel){
                log.info("查询角色{}",roleLabel);
                List<User> list = userService.selectByRole(roleLabel);
                return Result.success(list);
    }
}
